package com.gec.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysEstateCount;
import com.gec.model.vo.AssginRoleVo;
import com.gec.model.vo.SysEstateCountQueryVo;
import com.gec.model.vo.SysEstateCountSignInVo;
import com.gec.model.vo.UpdatePasswordVo;
import com.gec.system.annotation.MyLog;
import com.gec.system.service.SysEstateCountService;
import com.gec.system.util.MD5Helper;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统账号表 前端控制器
 * </p>
 *
 * @author lys
 * @since 2023-12-06
 */


@Api(tags = "账号管理控制器")
@RestController
@RequestMapping("/admin/system/sysEstateCount/")
public class SysEstateCountController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MsmController msmController;

    @Autowired
    private SysEstateCountService sysEstateCountService;

    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.OTHER,methodName = "查询账号列表")
    @ApiOperation("分页查询账号列表")
    @GetMapping("{page}/{limit}")
    public Result findPage(@PathVariable Long page,
                           @PathVariable Long limit,
                           SysEstateCountQueryVo vo){

        IPage<SysEstateCount> page1 = new Page<>(page,limit);
        page1 = this.sysEstateCountService.selectPage(page1,vo);
        return Result.ok(page1);
    }

    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.INSERT,methodName = "添加账号")
    @ApiOperation("添加账号")
    @PostMapping("addCount")
    public Result addCount(@RequestBody SysEstateCount sysEstateCount){

        System.out.println("uniApp添加账号："+sysEstateCount.getLoginName());
        //添加账号时对密码进行加密,使用md5
        String encrypt = MD5Helper.encrypt(sysEstateCount.getPassword());
        sysEstateCount.setPassword(encrypt);

        boolean save = this.sysEstateCountService.save(sysEstateCount);
        if (save){
            return Result.ok();
        }

        return Result.fail();
    }


    @ApiOperation("注册账号")
    @PostMapping("SignIn")
    public Result SignIn(@RequestBody SysEstateCountSignInVo sysEstateCount){

        System.out.println("uniApp添加账号："+sysEstateCount.getLoginName());
        //添加账号时对密码进行加密,使用md5
        String encrypt = MD5Helper.encrypt(sysEstateCount.getPassword());
        sysEstateCount.setPassword(encrypt);

        System.out.println(sysEstateCount.getTelephone());
        String phone = sysEstateCount.getTelephone();
        String code = (String) redisTemplate.opsForValue().get(phone);
        System.out.println("code"+code);
        String smsCode = sysEstateCount.getSmsCode();
        boolean flag = msmController.confirm(phone,smsCode);
        System.out.println("smsCode"+smsCode);
        if (flag){

        SysEstateCount sysEstateCount1=new SysEstateCount();
        sysEstateCount1.setLoginName(sysEstateCount.getLoginName());
        sysEstateCount1.setPassword(sysEstateCount.getPassword());
        sysEstateCount1.setTelephone(sysEstateCount.getTelephone());

        boolean save = this.sysEstateCountService.save(sysEstateCount1);
        if (save){
            return Result.ok();
        }
    }
        return Result.fail();
    }



    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.OTHER,methodName = "查询账号列表")
    @ApiOperation("根据id查询账号")
    @GetMapping("getCountById/{id}")
    public Result getCountById(@PathVariable Long id){
        QueryWrapper<SysEstateCount> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        SysEstateCount one = this.sysEstateCountService.getOne(wrapper);
        return Result.ok(one);
    }

    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.UPDATE,methodName = "修改账号")
    @ApiOperation("根据id修改账号")
    @PostMapping("updateCountById")
    public Result updateCountById(@RequestBody SysEstateCount count){


            count.setPassword(MD5Helper.encrypt(count.getPassword()));

        boolean b = this.sysEstateCountService.updateById(count);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.UPDATE,methodName = "修改账号密码")
    @ApiOperation("根据手机号修改账号密码")
    @PostMapping("updateCountPasswordByTel")
    public Result updateCountPasswordByTel(@RequestBody SysEstateCountSignInVo count){

        String phone = count.getTelephone();
        String smsCode = count.getSmsCode();

        boolean flag = msmController.confirm(phone,smsCode);

        if (flag){
            SysEstateCount sysEstateCount1=new SysEstateCount();
            sysEstateCount1.setLoginName(count.getLoginName());
            sysEstateCount1.setPassword(count.getPassword());
            sysEstateCount1.setTelephone(count.getTelephone());
            System.out.println(sysEstateCount1);
            boolean b=this.sysEstateCountService.updateByTel(sysEstateCount1);
            if (b){
                return Result.ok();
            }
        }

        return Result.fail();
    }



    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.UPDATE,methodName = "修改账号密码")
    @ApiOperation("根据新密码密码修改账号密码")
    @PostMapping("compareAndUpdatePwd")
    public Result compareAndUpdatePwd(@RequestBody UpdatePasswordVo vo){

        if (vo!=null){
            System.out.println(vo.getCountId());
            System.out.println(vo.getPrePwd());
            System.out.println(vo.getNewPwd());
        }
        return this.sysEstateCountService.compareAndUpdatePwd(vo);

    }


    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.DELETE,methodName = "删除账号")
    @ApiOperation("根据id删除账号")
    @DeleteMapping("removeCountById/{id}")
    public Result removeCountById(@PathVariable Long id){

        boolean b = this.sysEstateCountService.removeById(id);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }
    @MyLog(title = "账号模块",businessType = MyLog.BusinessType.DELETE,methodName = "删除账号")
    @ApiOperation("根据ids批量删除账号")
    @DeleteMapping("removeCountByIds")
    public Result removeCountByIds(@RequestBody List<Long> ids){

        boolean b = this.sysEstateCountService.removeByIds(ids);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }


}

